package com.qianfeng.qlm.handforireader.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by Administrator on 2015/3/3.
 */
public class NewImage extends ImageView {

    public NewImage(Context context) {
        super(context);
    }

    public NewImage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NewImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        switch (MeasureSpec.getMode(widthMeasureSpec)) {
            case MeasureSpec.EXACTLY:
                //指定宽高  match_parent 为此种类型
                break;
            case MeasureSpec.AT_MOST:
                //wrap_content 为此种类型  指定宽高 图片的宽度大于指定的宽高 则实际大小为指定的大小
                MeasureSpec.getSize(widthMeasureSpec);
                break;
            case MeasureSpec.UNSPECIFIED:
                //ListView GridView RecyclerView 传入时 为此 有多高显示多高
                break;


        }
        if (getDrawable() != null) {
            float width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) (width / getDrawable().getIntrinsicWidth() *
                    getDrawable().getIntrinsicHeight());
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
